package aaa.schedule;

import java.text.SimpleDateFormat;
import java.util.Locale;

import aaa.main.R;
import aaa.util.StringUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListLessonAdapter extends BaseAdapter {

	private Lessons lessons;

	private LayoutInflater inflater;

	public final ArrayAdapter<String> headers; 
	public final static int TYPE_SECTION_HEADER = 1;  
	public final static int TYPE_ITEM = 0; 

	public ListLessonAdapter(Context context, Lessons lessons) {
		inflater = LayoutInflater.from(context) ;
		this.createLessons(lessons) ;
		headers = new ArrayAdapter<String>(context, R.layout.list_header); 
	}

	private void createLessons(Lessons lessons) {
		this.lessons = new Lessons() ;
		this.lessons.ensureCapacity(lessons.size() * 2) ;
		String currentDate = null ;
		for(Lesson l : lessons) {
			String date = l.getTime() ;
			date = date.substring(0, date.indexOf(" ")) ;
			if (!date.equals(currentDate)) { //++d ;
				String formattedDate = new SimpleDateFormat("EEEE dd MMMM", Locale.FRANCE).format(l.getDate()) ;
				this.lessons.append(new DateSeparator(formattedDate)) ;
				currentDate = date ;
			}// if
			this.lessons.append(l) ;
		}// for
	}

	//méthode pour ajouter dans le header le nom de ma catégorie et dans sections le nom et un objet adapter
	public void addSection(String section/*, Adapter adapter*/) {  
		this.headers.add(section);
		//this.sections.put(section, adapter);  
	} 

	/*
         public int getViewTypeCount() {  
            int total = 1;  
           for(Adapter adapter : this.sections.values())  
               total += adapter.getViewTypeCount();  
           return total;  
       }  */

	@Override
	public int getItemViewType(int position) {  
		Lesson l = this.lessons.get(position) ;
		if (l instanceof DateSeparator) 
			return TYPE_SECTION_HEADER;
		else
			return TYPE_ITEM;           
	}

	public boolean areAllItemsSelectable() {  
		return false;  
	}  

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {  
		return (getItemViewType(position) != TYPE_SECTION_HEADER);  
	} 


	// * il nous faut spécifier la méthode "count()". 
	// * Cette méthode permet de connaitre le nombre d'items présent 
	// * dans la liste.	
	public int getCount() {
		return lessons.size();
	}


	// Permet de retourner un objet contenu dans la liste	
	public Object getItem(int index) {
		return this.lessons.get(index);
	}

	public long getItemId(int index) {
		return this.lessons.get(index).getLessonId();
	}

	public View getView(int position, View convertView, ViewGroup parent){
		Lesson l = this.lessons.get(position) ;
		if (l instanceof DateSeparator) {
			//if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_day_separator, null);
			//}// if

			String day = ((DateSeparator)l).getDay() ;
			TextView time = (TextView) convertView.findViewById(R.id.day);
			time.setText(StringUtils.capitalize(day)) ;

		} else {
			//if (convertView == null) {
			convertView = inflater.inflate(R.layout.lesson_view, null);
			//}// if
			TextView time  = (TextView) convertView.findViewById(R.id.time);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			TextView room  = (TextView) convertView.findViewById(R.id.room);

			title.setText(l.getTitle());
			time.setText(l.getTime().substring(l.getTime().indexOf(' ')+1));
			room.setText(l.getRoom()); 
		}// if

		return convertView;
	}

	public void setLesson(Lessons lessons) {
		this.createLessons(lessons) ;
		this.notifyDataSetChanged() ;
	}

}
